package jjn.carl.stack_queue;

import java.util.Stack;

/**
 * @author Jjn
 * @since 2023/7/7 22:50
 */
public class MyQueue {
    private final Stack<Integer> first;
    private final Stack<Integer> second;
    
    public MyQueue() {
        this.first = new Stack<>();
        this.second = new Stack<>();
    }
    
    public void push(int x) {
        first.push(x);
    }
    
    public int pop() {
        if (second.isEmpty()) {
            while (!first.isEmpty()) {
                second.push(first.pop());
            }
        }
        return second.pop();
    }
    
    public int peek() {
        if (second.isEmpty()) {
            while (!first.isEmpty()) {
                second.push(first.pop());
            }
        }
        return second.peek();
    }
    
    public boolean empty() {
        return first.isEmpty() && second.isEmpty();
    }
    
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println("myQueue.peek() = " + myQueue.peek());
        System.out.println("myQueue.pop() = " + myQueue.pop());
        System.out.println("myQueue.empty() = " + myQueue.empty());
    }
}
